/**
* Created by wust on 2019/5/14.
*/
<template>
  <div id="user-add">
    <el-form :inline="true" label-position="right" :model="searchModel" @submit.native.prevent  label-width="120px" size="mini">
      <el-form-item label="员工账号">
        <el-input v-model="searchModel.loginName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">检测</el-button>
      </el-form-item>
    </el-form>
    <el-table
      border
      size="mini"
      :data="baseDto.lstDto">
      <el-table-column
        prop="loginName"
        label="登录账号"
        width="150">
      </el-table-column>
      <el-table-column
        prop="realName"
        label="真实姓名"
        width="220">
      </el-table-column>
      <el-table-column
        prop="mobilePhone"
        label="联系电话"
        width="100">
      </el-table-column>
      <el-table-column
        prop="typeLabel"
        label="用户类型"
        width="120">
      </el-table-column>
      <el-table-column
        prop="email"
        label="邮箱"
        width="180">
      </el-table-column>
      <el-table-column
        prop="statusLabel"
        label="状态"
        width="80">
      </el-table-column>
      <el-table-column
        label="选择"
        width="80">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="onSubmit(scope.row)" :loading="submitting">确定</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import Vue from 'vue'

export default {
  name: 'user-add',
  props: ['selectedNode'],
  data () {
    return {
      submitting: false,
      searchModel: {
        loginName: null
      },
      baseDto: {},
    }
  },
  created: function () {
  },
  methods: {
    search: function () {
      if (Vue.$isNullOrIsBlankOrIsUndefined(this.searchModel.loginName)) {
        this.$message({
          message: '请输入登录账号',
          type: 'warning'
        })
        return
      }

      if (Vue.$isNullOrIsBlankOrIsUndefined(this.selectedNode)) {
        this.$message({
          message: '请选择岗位',
          type: 'warning'
        })
        return
      }

      Vue.$ajax({
        method: 'post',
        url: Vue.$adminServerURL + '/web/v1/OrganizationController/checkUser?loginName=' + this.searchModel.loginName + '&organizationId=' + this.selectedNode.id
      }).then(res => {
        if (res.data.flag !== 'SUCCESS') {
          this.$message({
            message: res.data.message,
            type: 'warning'
          })
          this.submitting = true
        } else {
          this.baseDto = res.data
          this.submitting = false
        }
      })
    },
    onSubmit: function (data) {
      this.submitting = true
      let organization = {pid: this.selectedNode.id, type: 'A101115', relationId: data.id}
      Vue.$ajax({
        method: 'post',
        url: Vue.$adminServerURL + '/web/v1/OrganizationController/addUser',
        data: {organization: organization}
      }).then(res => {
        this.submitting = false
        if (res.data.flag !== 'SUCCESS') {
          if (!Vue.$isNullOrIsBlankOrIsUndefined(res.data.message)) {
            this.$message({
              message: res.data.message,
              type: 'warning'
            })
          }
        } else {
          this.$message({
            message: res.data.message,
            type: 'success'
          })

          let name = data.realName
          let newNode = {id: res.data.obj.id, pId: this.selectedNode.id, name: name, type: 'A101115', relationId: data.id}
          this.addNode(newNode)
        }
      })
    },
    addNode: function (newNode) {
      this.$emit('addNode', newNode)
    }
  }
}
</script>
